package cn.kwq.project_manger.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
import cn.kwq.project_manger.chat.ChatServiceImpl;
import cn.kwq.project_manger.chat.GetChatDto;
import cn.kwq.project_manger.chat.WebSocket;
import cn.kwq.project_manger.common.R;
import cn.kwq.project_manger.common.RES;
import cn.kwq.project_manger.pojo.Conv;
import cn.kwq.project_manger.pojo.dto.*;
import cn.kwq.project_manger.service.MsgService;
import com.alibaba.fastjson2.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(tags ="消息推送服务")
@RestController
@RequestMapping("/Msg/")
public class MsgController {

    @Autowired
    MsgService msgService;
    @Resource
    private WebSocket webSocket;
    @Autowired
    ChatServiceImpl chatService;

    @ApiOperation("推送消息")
    @PostMapping("Send")
    @SaCheckLogin
    public RES Send(@RequestBody SendMsg dto) {
        return R.ok("推送成功",msgService.Send(dto));
    }

    @ApiOperation("获取本用户的所有推送消息")
    @GetMapping("GetMsg")
    @SaCheckLogin
    public RES GetMsg() {
        return R.ok("获取成功",msgService.getMsg(StpUtil.getLoginIdAsLong()));
    }

    @ApiOperation("某消息设置为已读")
    @PostMapping("Read")
    @SaCheckLogin
    public RES Read(@ApiParam(name = "msgId", value = "消息的id", required = true)
                         @RequestBody Map<String,Long> object) {
        Long msgId= (Long) object.get("msgId");
        return R.ok("已读",msgService.Read(msgId));
    }

    @ApiOperation("获取未读消息数量")
    @GetMapping("countUnRead")
    @SaCheckLogin
    public RES countUnRead() {
        Map<String,Object> map=new HashMap<>();
        map.put("total",msgService.CountUnRead(StpUtil.getLoginIdAsLong()));
        map.put("time",new DateTime());
        return R.ok(map);
    }

    @ApiOperation("发送socket对话消息")
    @PostMapping("sendChat")
    @SaCheckLogin
    public RES sendChat(@RequestBody ConvMsgDto dto) throws IOException {
        //发送
        webSocket.sendInfo(dto.getMsg(),dto.getToId(),StpUtil.getLoginIdAsLong());
        //持久化
        chatService.sendMsg(StpUtil.getLoginIdAsLong(),dto.getToId(),dto.getMsg());
        return R.ok("推送成功");
    }

    @ApiOperation("获取socket历史消息（通过对话人的id）")
    @PostMapping("getHistory")
    @SaCheckLogin
    public RES getHistory(@RequestBody GetChatDto obj) {
        return R.ok(chatService.findAllChatMsgList(StpUtil.getLoginIdAsLong(), obj.getToId(), obj.getPageSize(), obj.getPageNum()));
    }
    @ApiOperation("获取全部消息")
    @GetMapping("getAllChat")
    @SaCheckLogin
    public RES getAllChat() {
        return R.ok(chatService.findAllChatMsgList(StpUtil.getLoginIdAsLong()));
    }





}
